package pl.edu.utp.ga.genes;

import java.util.Random;

import pl.edu.utp.calculations.Range;

public class FloatingGene extends BasicGene<Double>{
	
	private Random random = new Random();
	private Range range;
	
	public FloatingGene(){
		generate();
	}
	
	public FloatingGene(Range range){
		this.range = range;
		generate();
	}
	
	public FloatingGene(Double gene){
		this.gene = gene;
	}
	
	public Range getRange() {
		return range;
	}

	public void setRange(Range range) {
		this.range = range;
	}

	@Override
	public Double generate() {
		if(range == null){
			gene = random.nextDouble()*Double.MAX_VALUE-(Double.MAX_VALUE/2);
		} else{
			gene = random.nextDouble()*(range.getTo()-range.getFrom())+range.getFrom();
		}
		return gene;
	}
		
	@Override
	public void mutate(Double mutation){
		if(random.nextBoolean())
			gene = gene + mutation;
		else
			gene = gene - mutation;
	}
	
	public String toString(){
		return String.format("%.4f", gene);
	}
}